﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.MembershipInterface.ObjectModel;

using Microsoft.Crm.Sdk;


namespace CSP.CRM.Integration
{

  /// <summary>
  /// Helper class for interaction with CRM dynamic entities and the MembershipInterface
  /// object model.
  /// </summary>
  class Util
  {
    public static void AddStringProp(DynamicEntity de, string name, string value)
    {
      if (value != null)
        de.Properties.Add(new StringProperty(name, value));
    }

    public static void AddDecimalProp(DynamicEntity de, string name, string value)
    {
      if (value != null)
        de.Properties.Add(new CrmDecimalProperty(name, new CrmDecimal(Convert.ToDecimal(value))));
    }

    public static void AddNumberProp(DynamicEntity de, string name, string value)
    {
      if (value != null)
        de.Properties.Add(new CrmNumberProperty(name, new CrmNumber(Convert.ToInt32(value))));
    }

    public static string GetStringProp(DynamicEntity de, string name)
    {
      if (de.Properties.Contains(name))
        return (string)de.Properties[name];
      else
        return null;
    }

    public static string GetNumberProp(DynamicEntity de, string name)
    {
      if (de.Properties.Contains(name))
        return ((CrmNumber)de.Properties[name]).Value.ToString();
      else
        return null;
    }

    public static CaseType GetCaseType(DynamicEntity de, string name)
    {
      if (de.Properties.Contains(name))
      {
        Picklist pl = (Picklist)de.Properties[name];
        CaseType caseType = new CaseType();
        caseType.CaseTypeTitle = pl.name;
        caseType.CaseTypeId = pl.Value;
        return caseType;
      }
      else
        return null;
    }

    private static string GetDateTimeProp(DynamicEntity de, string name)
    {
      if (de.Properties.Contains(name))
        return ((CrmDateTime)de.Properties[name]).Value;
      else
        return null;
    }


    public static CaseStatus GetCaseStatus(DynamicEntity de, string name)
    {
      if (de.Properties.Contains(name))
      {
        Status status = (Status)de.Properties[name];
        CaseStatus caseStatus = new CaseStatus();
        caseStatus.CaseStatusTitle = status.name;
        caseStatus.CaseStatusId = status.Value;
        return caseStatus;
      }
      else
        return null;
    }

    public static string GetKey(DynamicEntity de, string name)
    {
      if (de.Properties.Contains(name))
      {
        Key key = (Key)de.Properties[name];
        return key.Value.ToString();
      }
      else
        return null;
    }


    public static Case CreateFromCrmCase(DynamicEntity crmCase)
    {
      CaseType ct = Util.GetCaseType(crmCase, "csp_proceduretype");
      if (ct == null)
        return null;
      Case _case = new Case();
      _case.ID = Util.GetKey(crmCase, "incidentid");
      _case.Type = ct;
      _case.Title = Util.GetStringProp(crmCase, "ticketnumber");
      _case.Description = _case.Type.CaseTypeTitle;
      _case.Status = Util.GetCaseStatus(crmCase, "statuscode");
      string strDueDate = Util.GetDateTimeProp(crmCase, "followupby");
      if (strDueDate != null)
        _case.DueDate = DateTime.Parse(strDueDate);
        
      return _case;
    }

    public static Citizen CreateCitizenFromCrmContact(DynamicEntity contact)
    {
      Citizen citizen = new Citizen();
      string strCreatedDate = Util.GetDateTimeProp(contact, "createdon");
      if (strCreatedDate != null)
        citizen.CreatedDate = DateTime.Parse(strCreatedDate);

      string strLastModifiedDate = Util.GetDateTimeProp(contact, "lastmodifiedon");
      if (strLastModifiedDate != null)
        citizen.LastModified = DateTime.Parse(strLastModifiedDate);

      citizen.Address = Util.GetStringProp(contact, "address1_line1");
      citizen.Children = Util.GetNumberProp(contact, "numberofchildren");
      citizen.City = Util.GetStringProp(contact, "address1_city");
      citizen.DisplayName = Util.GetStringProp(contact, "fullname");
      citizen.ElderlyRelatives = Util.GetNumberProp(contact, "csp_elderlyrelatives");
      citizen.EmailAddress = Util.GetStringProp(contact, "emailaddress1");
      citizen.FirstName = Util.GetStringProp(contact, "firstname");
      citizen.LastName = Util.GetStringProp(contact, "lastname");
      citizen.Phone = Util.GetStringProp(contact, "telephone1");
      citizen.PostalCode = Util.GetStringProp(contact, "address1_postalcode");
      citizen.Username = Util.GetStringProp(contact, "csp_username");
      citizen.PhotoUrl = Util.GetStringProp(contact, "csp_picture");

      return citizen;
    }

    public static CitizenRequest CreateCitizenRequestFromCrmContact(DynamicEntity contact)
    {
      CitizenRequest citizenRequest = new CitizenRequest();
      string strCreatedDate = Util.GetDateTimeProp(contact, "createdon");
      if (strCreatedDate != null)
        citizenRequest.CreatedDate = DateTime.Parse(strCreatedDate);

      string strLastModifiedDate = Util.GetDateTimeProp(contact, "lastmodifiedon");
      if (strLastModifiedDate != null)
        citizenRequest.LastModified = DateTime.Parse(strLastModifiedDate);

      citizenRequest.Address = Util.GetStringProp(contact, "address1_line1");
      citizenRequest.Children = Util.GetNumberProp(contact, "numberofchildren");
      citizenRequest.City = Util.GetStringProp(contact, "address1_city");
      citizenRequest.DisplayName = Util.GetStringProp(contact, "fullname");
      citizenRequest.ElderlyRelatives = Util.GetNumberProp(contact, "csp_elderlyrelatives");
      citizenRequest.EmailAddress = Util.GetStringProp(contact, "emailaddress1");
      citizenRequest.FirstName = Util.GetStringProp(contact, "firstname");
      citizenRequest.LastName = Util.GetStringProp(contact, "lastname");
      citizenRequest.Phone = Util.GetStringProp(contact, "telephone1");
      citizenRequest.PostalCode = Util.GetStringProp(contact, "address1_postalcode");
      citizenRequest.Username = Util.GetStringProp(contact, "csp_username");
      citizenRequest.Password = Util.GetStringProp(contact, "csp_password");
      citizenRequest.PhotoUrl = Util.GetStringProp(contact, "csp_picture");
      
      citizenRequest.RequestType = new CitizenRequestType();
      citizenRequest.RequestType = CitizenRequestType.Add;


      return citizenRequest;
    }


  }
}
